using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using mojoPortal.Web.Controls;
using mojoPortal.Web.UI;
using mojoPortal.Web.Editor;
using mojoPortal.Net;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Word;


public partial class MyCustomModules_FileMyReturns : System.Web.UI.UserControl
{
    int iPageID = CacheHelper.GetCurrentPage().PageId;
    string conn = System.Configuration.ConfigurationManager.AppSettings.Get("MSSQLConnectionString");
    int UserID;
    Guid uguid;

    protected void ShowCalendar(object sender, EventArgs e)
    {      

        clnDOB.Visible = true;   
    }

    protected void clnDOB_SelectionChanged(object sender, EventArgs e)
    {
        txtDOB.Text = clnDOB.SelectedDate.ToString("dd-MMM-yy");
        clnDOB.Visible = false;   
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Populate dropdown list for year
        int iYear = DateTime.Now.Year - 5;
        int total = 0;
        for (int count = 0; count < 15; count++)
        {
            total = iYear + count;
            drpYears.Items.Add(total.ToString());

        }
       

        if (!IsPostBack)
        {
            CollapsiblePanelExtender1.Collapsed = true;
            CollapsiblePanelExtender2.Collapsed  = true;

            drpYears.SelectedValue = DateTime.Now.Year.ToString();
            
            // Populate personal and address details from database
            PopulateFields();

        }
    }

    protected void PopulateFields()
    {
        System.Data.SqlClient.SqlDataReader dr =  mojoPortal.Data.SqlHelper.ExecuteReader 
            (conn, CommandType.StoredProcedure , "usp_GetUserDetails", 
            new System.Data.SqlClient.SqlParameter("@UserEmail", HttpContext.Current.User.Identity.Name.ToString() ));
        if (dr.HasRows)
        {
            
            dr.Read();

            uguid = new Guid(dr["UserGuid"].ToString());
            UserID = Convert.ToInt32(dr["UserID"]);

            txtName.Text =  dr["Name"].ToString();
            txtFName.Text = dr["FName"].ToString();
            txtPAN.Text = dr["PAN"].ToString();
            if (dr["DOB"] != null)
            {
                DateTime dtTemp;
                if ( DateTime.TryParse(dr["DOB"].ToString(), out dtTemp))
                    txtDOB.Text = dtTemp.ToString("dd/MM/yyyy");
            }
        }
        dr.Close();
        
        
    }
    protected void btnUpdatePersonalDetails_Click(object sender, EventArgs e)
    {
         
        System.Threading.Thread.Sleep(2000);

    }
    protected void btnUpdateAddress_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);
    }
    protected void btnUploadForm16_Click(object sender, EventArgs e)
    {
        // Get User Details
        System.Data.SqlClient.SqlDataReader dr = mojoPortal.Data.SqlHelper.ExecuteReader
           (conn, CommandType.StoredProcedure, "usp_GetUserDetails",
           new System.Data.SqlClient.SqlParameter("@UserEmail", HttpContext.Current.User.Identity.Name.ToString()));
        if (dr.HasRows)
        {

            dr.Read();

            uguid = new Guid(dr["UserGuid"].ToString());
            UserID = Convert.ToInt32(dr["UserID"]);
        }
        dr.Close();

             
        //  int result = Convert.ToInt32(sph.ExecuteScalar());
        if ( uplForm16.HasFile)
        {
            string filename = Path.GetFileName(uplForm16.FileName);
            uplForm16.SaveAs(Server.MapPath("~/") + Common.GetNewFileName(UserID, filename, drpYears.SelectedValue)); 
           
            mojoPortal.Data.SqlParameterHelper sph = new mojoPortal.Data.SqlParameterHelper(conn, "usp_AddNewDocument", CommandType.StoredProcedure, 6);
            sph.DefineSqlParameter("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID);
            sph.DefineSqlParameter("@UserGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, uguid);
            sph.DefineSqlParameter("@ParentGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, uguid);
            sph.DefineSqlParameter("@Year", SqlDbType.Int, ParameterDirection.Input, drpYears.SelectedValue);
            sph.DefineSqlParameter("@FilePath", SqlDbType.NVarChar, 1000, ParameterDirection.Input, Common.GetNewFileName(UserID, filename, drpYears.SelectedValue) );
            sph.DefineSqlParameter("@DocType", SqlDbType.NVarChar, 50, ParameterDirection.Input,
                Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings.Get("ITRV") ));

            int result = Convert.ToInt32(sph.ExecuteNonQuery  ());

           
            if (result == -1)
            {
                lblMessage.Text = "Form16 already uploaded for this year.";
            }
            else if (result > 0)
            {
                MailMerge msg = new MailMerge(txtName.Text, "friend's name", "friend's email address",
                    txtPAN.Text, drpYears.SelectedValue, filename, Common.GetNewFileName(UserID, filename, drpYears.SelectedValue)
                    , "DocUpload");
                 //send mail to uploader
                Common.SendMailMessage(HttpContext.Current.User.Identity.Name.ToString(),
                    "", System.Configuration.ConfigurationManager.AppSettings.Get("SMTPTo"), msg.Subject, msg.Body);
               // MailHelper.SendMailMessage(HttpContext.Current.User.Identity.Name.ToString(), 
               //     "", System.Configuration.ConfigurationManager.AppSettings.Get("SMTPTo"), msg);
                
                //TODO: Send mail to admin with a different template
                ////msg = new MailMerge(txtName.Text, "friend's name", "friend's email address",
                ////    txtPAN.Text, drpYears.SelectedValue, filename, GetNewFileName(UserID, filename), "DocUpload");
                ////MailHelper.SendMailMessage(HttpContext.Current.User.Identity.Name.ToString(), "", "", msg);
                Response.Redirect(Request.Url.ToString());
            }
            else
                lblMessage.Text = "Error occured";
        }

     
    }

   
     

}
